<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cat extends MY_Controller {
	private $list_cat;
	public function __construct(){
		parent::__construct();
		$this->list_cat = $this->cm->get_all($this->tbl->cat,array('mid'=>$this->mid),'',0,'a');
		$this->load->model('model_model','model');
	}
	
	function ajax_add(){

		$r = array();
		$r['pid'] = !empty($this->p['pid']) ? $this->p['pid'] : 0;
		$this->_data['r'] = (object)$r;

		$this->_data['opt_cat'] = getCatOpt($this->list_cat);

		$this->load->view('cat/add',$this->_data);
	}
	
	function ajax_edit(){
		$id = $this->p['id'];

		$r = $this->cm->get_by_id($this->tbl->cat,$id);
		if(empty($r)) die;

		$this->_data['r'] = $r;

		$this->_data['opt_cat'] = getCatOpt($this->list_cat);
		$this->load->view('cat/add',$this->_data);

	}
	
	function index(){
		$this->_data['tree_cat'] = getCatTree($this->list_cat);
		$this->_data['opt_model'] = $this->model->get_opt();
		$this->load->view('cat/index',$this->_data);
	}
	
	function save_do(){
		$this->cm->save($this->tbl->cat,$this->p);
		ejd('succ-reload','操作成功！');
	}
	
	function del_do(){
		$id = $this->p['id'];

		//有子分类禁止删除操作
		$count_sub = $this->cm->count($this->tbl->cat,array('pid'=>$id));
		if($count_sub>0) ejd('fail','非叶子节点不能删除！');

		$this->cm->delete($this->tbl->cat,array('id'=>$id));
		ejd('succ-reload','操作成功！');
	}
}

